# -*- coding: UTF-8 -*-
"""
@auth:buxiangjie
@date:2022/10/27 15:20
@describe: 
"""
import asyncio

from app.log.ulog import logger_
from app.db.db_api import ToolsSql
from aiocache import cached

class DataBasePool:
	__instance = None

	def __new__(cls, *args, **kwargs):
		if not cls.__instance:
			cls.__instance = object.__new__(cls)
		return cls.__instance

	@cached()
	async def init(self):
		self.test_saas = await ToolsSql.conn_db_pool("test", "saas")
		self.qa_saas = await ToolsSql.conn_db_pool("qa", "saas")
		self.test_data_station = await ToolsSql.conn_db_pool("test", "data_station")
		self.qa_data_station = await ToolsSql.conn_db_pool("qa", "data_station")
		self.test_nebula = await ToolsSql.conn_db_pool("test", "nebula")
		self.qa_nebula = await ToolsSql.conn_db_pool("qa", "nebula")
		self.test_statistics = await ToolsSql.conn_db_pool("test", "statistics")
		logger_.info("数据库初始化成功")
		return self

	async def tear_down(self):
		self.test_saas.close()
		await self.test_saas.wait_closed()
		self.qa_saas.close()
		await self.qa_saas.wait_closed()
		self.test_data_station.close()
		await self.test_data_station.wait_closed()
		self.qa_data_station.close()
		await self.qa_data_station.wait_closed()
		self.test_nebula.close()
		await self.test_nebula.wait_closed()
		self.qa_nebula.close()
		await self.qa_nebula.wait_closed()
		self.test_statistics.close()
		await self.test_statistics.wait_closed()
		logger_.info("关闭连接池完成")
